<?php

function list_kenaikan(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
		
		function confirmDeleteRapel( i){
		var check = confirm(\'Akan mendelete data rapel? \'); 
		if(check){
			location.href=\'index.php?com=pr_rapel&task=rapel_delete&id=\'+i;
		}
		return;	
		}		
	');

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nomor SK'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Periode'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Cola (%)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		'Rapel'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Proses'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);
	$query = "SELECT * FROM pr_kenaikan_gaji ORDER BY kenaikan_gaji_id DESC LIMIT 0 , 20";
	$result = my_query($query);
	$row = array();
	$i=0;
	while( $ey = my_fetch_array($result) ){
		$i++;
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_detail&id='.$ey['kenaikan_gaji_id'],
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$edit_property = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=kenaikan_create&id='.$ey['kenaikan_gaji_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $edit_property  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['kenaikan_gaji_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nomor'		=>  $ey['sk_no'] ,   
			'periode'	=> position_text_align ( date("m-Y", strtotime($ey['tanggal']   ) ), 'left'), 
			'cola'		=> position_text_align ( $ey['cola'] , 'right'), 
			'rapel'		=> position_text_align (  $ey['rapel'], 'center'),  
			'proses'	=> position_text_align (  $ey['proses'], 'center'),  
			'operasi'	=> position_text_align(  $edit_button.' '. $detail_button .' '.$delete_button, 'right'),  
		); 
	}
	
	$datas = table_rows($row); 
	$view  ='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$periode = my_get_data_by_id('pr_periode','periode_id',get_periode_gaji() );
	$tanggal = $periode['tanggal'];	
	$temp=(int) date('m' , strtotime($tanggal));
	$view .= form_field_display( '<span class="label_form"> '.sprintf( '%02d',$temp).'-'.date('Y' , strtotime($tanggal)).'</span>'   , "<b>Periode Gaji</b>"    ); 
	
	$menuontop = array(  
		'Tambah data'=> array('onclick'=> 'javascript:jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=kenaikan_create&id=0\' })')
	);	
	$printmenu = additional_menu_on_list( $menuontop);
	return $view.$printmenu.table_builder($header , $datas ,  5 , false );
}
function form_kenaikan_create($id = 0){
	
	$view = form_header( "tahun" , "tahun"  );
	if($id>0){
		$fields = my_get_data_by_id( 'pr_kenaikan_gaji' , 'kenaikan_gaji_id' , $id );
		$month = date('m',strtotime($fields['tanggal']));
		$year = date('Y',strtotime($fields['tanggal']));
	}else{
		$month = date('m');
		$year = date('Y');
	} 

	$sk_no = array(
			'name'=>'sk_no',
			'value'=>(isset($_POST['sk_no'])? $_POST['sk_no'] : $fields['sk_no']),
			'id'=>'sk_no',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_nomor = form_dynamic($sk_no);
	$view .= form_field_display( $form_nomor  , "Nomor SK"  );
	
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );
	$view .= form_field_display( $bulan_form.' '. $tahun_form  , "Periode"    );

	$cola = array(
			'name'=>'cola',
			'value'=>(isset($_POST['cola'])? $_POST['cola'] : $fields['cola']),
			'id'=>'cola',
			'type'=>'textfield',
			'size'=>'10'
		);
	$form_nomor = form_dynamic($cola);
	$view .= form_field_display( $form_nomor.' %'  , "Cola"  );


	if((isset($_POST['rapel'])? $_POST['rapel'] : $fields['rapel'])=="Y"){ 
		$rapel = array(
			'name'=>'rapel',
			'id'=>'rapel',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$rapel = array(
			'name'=>'rapel',
			'id'=>'rapel',
			'type'=>'checkbox',
		); 
	}
	$form_rapel = form_dynamic($rapel).'Rapel';
	$view .= form_field_display( $form_rapel , "" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit',
		'class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function last_gapok($karyawan_id , $last_periode_id){
	
	$query =   " SELECT gaji_dibayar FROM pr_gaji 
			WHERE  periode_id = {$last_periode_id}  AND karyawan_id ={$karyawan_id} ";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['gaji_dibayar'];
}

function kenaikan_validate($id){
	$errsubmit = false;
	$err = array();

	$temp=(int) $_POST['cola'] ;
	if( $temp<=100){
		$errsubmit = true;
		$err[] = "Cola harus > 100%";	
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function form_kenaikan_submit($id){
	$rapel = isset($_POST['rapel']) ? 'Y': 'T';
	$tanggal = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	$datas = array(
		'sk_no'=>my_type_data_str($_POST['sk_no']),
		'tanggal'=> my_type_data_str($tanggal),
		'cola'=>my_type_data_str($_POST['cola']),
		'rapel'=>my_type_data_str($rapel),
		'proses'=>my_type_data_str('T'),
	);
	
	if($id > 0){
		my_update_record( 'pr_kenaikan_gaji' , 'kenaikan_gaji_id' , $id , $datas);
	}else{
		$id=my_insert_record( 'pr_kenaikan_gaji' , $datas );
	}
	if($rapel=="Y"){
		insert_rapel(1, $id) ;
		reset_rapel_bayar(1, $id) ;
	}else{
		reset_rapel(1, $id) ;
	}
	return true;
}
 
function delete_kenaikan($id){
	reset_rapel(1, $id) ;
	$query = "DELETE FROM pr_kenaikan_gaji WHERE kenaikan_gaji_id ={$id} ";
	my_query($query);
	$query = "DELETE FROM pr_kenaikan_gaji_karyawan WHERE kenaikan_gaji_id ={$id} ";
	return my_query($query);
}	


function list_kenaikan_detail($id){
	$pr_kenaikan_gajis = my_get_data_by_id( 'pr_kenaikan_gaji' , 'kenaikan_gaji_id' , $id );
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:3%'), 
		'nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:7%'),   
		'nama'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'grade'=>array('style'=>'text-align:right;border-bottom:2px solid;width:5%'),  
		'gaji sekarang'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
	 	'^Adj/='=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
	 	'PA'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'), 
	 	'Mark2'=>array('style'=>'text-align:left;border-bottom:2px solid;width:5%'), 
	 	'GB+PA'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
	 	'COLA'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key']))
			$query = "SELECT * FROM pr_kenaikan_gaji_karyawan a 
				INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
				INNER JOIN biodata c ON c.biodata_id = b.biodata_id
				WHERE kenaikan_gaji_id = {$id} AND ( b.nik='{$_GET['key']}' OR c.nama LIKE '%{$_GET['key']}%'  )
				ORDER BY b.nik";
		else
			$query = "SELECT * FROM pr_kenaikan_gaji_karyawan a 
				INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
				WHERE kenaikan_gaji_id = {$id} 
				ORDER BY b.nik"; 
	
			
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&id={$_GET['id']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$detailproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=list_karyawan&id='.$id.'&karyawan_id='.$ey['karyawan_id'],
				'title'=>'Detail periode'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),
			'nik'=>position_text_align( $ey['nik'], 'center'),
			'nama'=>$karyawan['nama_gelar'], 
			'grade'=>position_text_align($ey[grade_id], 'right'),   
			'gaji_lama'=>position_text_align( rp_format($ey['gaji_lama']) , 'right'),   
			'adj'=> position_text_align( rp_format($ey['adj']) , 'right'),   
			'pa'=>position_text_align( $ey['pa'] , 'center'),   
			'mark2'=>position_text_align($ey['mark2'] , 'left'),   
			'gb_pa'=>position_text_align( rp_format($ey['gb_pa']) , 'right'),   
			'gaji_baru'=>position_text_align( rp_format($ey['gaji_baru']) , 'right'),   
			' v'=>position_text_align($detail_button,'right'),
			);
	}
	$datas = table_rows($row); 
	$menuontop = array(  
		'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_kenaikan_gaji'.'\''),
		//'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&id='.$id.'&key='.$key.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&id='.$id.'&key='.$key.'\';'),
		'Exception' => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=pr_kenaikan_gaji&task=exception&id='.$id.'\' });'),
		($pr_kenaikan_gajis['proses']=="Y" ? 'Reproses' : 'Proses') => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=pr_kenaikan_gaji&task=loadpage&id='.$id.'\' });'),
	);
	$search = '<form method="GET">
	<input type="hidden" name="com" value="'.$_GET['com'].'" />
	<input type="hidden" name="task" value="'.$_GET['task'].'" />
	<input type="hidden" name="id" value="'.$_GET['id'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	
	$view  = form_header( "..." , "..."  );
	$view .=form_field_display( '<span class="label_form">'. $pr_kenaikan_gajis['sk_no'] .'</span>'   , "<b>Nomor SK</b>"    ) ;
	$view .=form_field_display( '<span class="label_form">'.  date( "m-Y" , strtotime($pr_kenaikan_gajis['tanggal'])) .'</span>'   , "<b>Periode</b>"    ) ;
	$view .=form_field_display( '<span class="label_form">'.  $pr_kenaikan_gajis['cola'].'%</span>'   , "<b>Cola</b>"    ) ;
	$view  .= form_footer(  );
	
	$printmenu = additional_menu_on_list( $menuontop , $search);
	return $view.$printmenu. table_builder($header , $datas ,  11 , false ,$kgPagerOBJ ->showPaging() );	
}


function page_kalkulasi_kenaikan($id){
	my_set_code_js('
		$(document).ready(function() {
			$(\'#onebar\').load(\'index.php?com='.$_GET['com'].'&task=proses&id='.$id.'&'.rand(0,9).'='.rand(0,99999).'\'); 
		});
	');
	return '<div id="onebar" style="width:100%;text-align:center;"><img src="components/system/jquery/facebox/loading.gif" /><br/>On processing!!!</div>';
}

function log_kenaikan_gaji($karyawan_id ,$kenaikan_gaji_id , $gapoklama ,$gapokbaru){ 
	$query = "DELETE FROM pr_kenaikan_gaji_log WHERE karyawan_id = {$karyawan_id} AND kenaikan_gaji_id ={$kenaikan_gaji_id} ";
	my_query($query);
	$datas = array(
		'karyawan_id' 		=> my_type_data_int($karyawan_id), 	 	 	 	
		'kenaikan_gaji_id' 		=> my_type_data_int($kenaikan_gaji_id),
		'gapok_lama' 		=> my_type_data_str($gapoklama),
		'gapok_baru'	 	=> my_type_data_str($gapokbaru),
		'datetime_added'	=> my_type_data_function('NOW()')
	);
	return my_insert_record(	'pr_kenaikan_gaji_log'	, 	$datas );
}

function update_new_gapok($karyawan_id , $gapok_br){
	$datas = array(
		'gapok_km'	=> my_type_data_str($gapok_br)
	);
	
	return my_update_record(	'kekaryawanan'  ,	'karyawan_id'	,	$karyawan_id	,	$datas );
}

function data_karyawan_exists($id , $karyawan_id){
	$query = "SELECT * FROM pr_kenaikan_gaji_karyawan WHERE karyawan_id = {$karyawan_id} AND kenaikan_gaji_id={$id} ";
	$result = my_query($query);
	if(my_num_rows($result) > 0){
		return true;
	}
	return false;
}
	
function update_kenaikan_karyawan($id){ 

	$pr_kenaikan_gajis = my_get_data_by_id( 'pr_kenaikan_gaji' , 'kenaikan_gaji_id' , $id );
	$query = "DELETE FROM pr_kenaikan_gaji_karyawan WHERE kenaikan_gaji_id={$id} AND mark2 <> 'TP'";
	my_query($query);	
		
	$query = "
		SELECT struktur_gaji_id FROM pr_struktur_gaji_tahun 
		WHERE tanggal< '{$pr_kenaikan_gajis['tanggal']}'
		ORDER BY tanggal DESC LIMIT 1
	";
		
	$struktur_gajis=my_fetch_array(my_query($query));
	$struktur_gaji_id = isset( $struktur_gajis['struktur_gaji_id'] ) ? $struktur_gajis['struktur_gaji_id'] :  0;
	
	$query_last_periode = "SELECT MAX(periode_id) AS last_periode_id FROM pr_gaji LIMIT 1";
	$result_last_periode = my_query($query_last_periode);
	$row_last_periode = my_fetch_array($result_last_periode);
	$last_periode_id = $row_last_periode['last_periode_id'];
	revert($id); 
	$query = "
		SELECT * FROM karyawan a 
		INNER JOIN kekaryawanan b ON b.karyawan_id=a.karyawan_id 
		INNER JOIN struktur_gaji_detail c ON c.grade_id=b.grade_id AND c.struktur_gaji_id={$struktur_gaji_id}
		WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 
		ORDER BY a.nik
	";
	$result = my_query($query );  
	
	while($ey = my_fetch_array($result)){
		$karyawan_id=$ey['karyawan_id'] ;
		if( data_karyawan_exists($id , $karyawan_id))continue;
		

		$karyawan = loaddata_karyawan($karyawan_id);
		$grade_id=$karyawan['grade_id'] ;
		$gaji_lama = last_gapok($karyawan_id , $last_periode_id);
		$adj=$gaji_lama ;
		if( (int) $gaji_lama< (int) $ey['min']){;
			$adj=min($ey['min'],$gaji_lama*1.15) ;
		}
		$query_nilai = "SELECT evaluasi_nilai_id FROM penilaian_karyawan 
			WHERE karyawan_id = {$karyawan_id} ORDER BY penilaian_id DESC LIMIT 1";
		$result_nilai = my_query($query_nilai);
		$evaluasi_nilai_karyawan = my_fetch_array($result_nilai);
		//$evaluasi_nilai_karyawan = my_get_data_by_id('penilaian_karyawan','karyawan_id',$karyawan_id );
		$evaluasi_nilai_id=(isset($evaluasi_nilai_karyawan['evaluasi_nilai_id']) ? $evaluasi_nilai_karyawan['evaluasi_nilai_id'] :0 );
		$evaluasi_nilais = my_get_data_by_id('evaluasi_nilai','evaluasi_nilai_id',$evaluasi_nilai_id );
		 
		/*if( (int) $gaji_lama >= (int) $ey['max'] ){
			$mark2="MTK-1" ;
			$gb_pa=$gaji_lama ;
			$lumps=($evaluasi_nilais['mid_point']/100* (int) $ey['mid'])*$evaluasi_nilais['lumpsum'] ;
		}else{
			if( ( $gaji_lama+( $evaluasi_nilais['mid_point']/100* (int) $ey['mid']) ) > (int) $ey['max']){
				$mark2="MTK-2" ;
				$gb_pa= (int) $ey['max'] ;
				$lumps=(($evaluasi_nilais['mid_point']/100* (int) $ey['mid']) 
					+ $gaji_lama - $gb_pa)
					*$evaluasi_nilais['lumpsum'] ;
			}else{
				$mark2="NA" ;	
				$lumps=0 ;
				$gb_pa=(int) $gaji_lama+( $evaluasi_nilais['mid_point']/100* (int) $ey['mid']) ;
			}	
		}
		*/
		
		//LOGIKA TANPA MARK DEFAULT
		$mark2="NA" ;	
		$lumps=0 ;
		//$gb_pa=(int) $gaji_lama+( $evaluasi_nilais['mid_point']/100* (int) $ey['mid']) ;
		if($karyawan['status_kekaryawan_id'] == 2)
			$gb_pa=$gaji_lama;
		else
			$gb_pa=(int) $adj+( $evaluasi_nilais['mid_point']/100* (int) $ey['mid']) ;
		//LOGIKA END		
		
		$gb_pa=round($gb_pa,0);
		// MPP ? =IF(S5="NA",Q5+VLOOKUP(O5,TBL!$K$3:$L$8,2)*M5,IF(S5="MTK-1",Q5,IF(S5="MPP",Q5,N5)))
		
		$cola= $pr_kenaikan_gajis['cola'];
 		$gaji_baru=round($gb_pa*$cola/100,0);
		$datas = array(
			'kenaikan_gaji_id'=>my_type_data_int($id),
			'karyawan_id'=>my_type_data_int($karyawan_id),
			'grade_id'=>my_type_data_int($grade_id),
			'gaji_lama'=>my_type_data_str( $gaji_lama ), 			
			'adj'=>my_type_data_str( $adj ), 			
			'pa'=>my_type_data_str( $evaluasi_nilais['huruf'] ), 			
			'mark2'=>my_type_data_str( $mark2 ), 			
			'gb_pa'=>my_type_data_str( $gb_pa ), 			
			'lumps'=>my_type_data_str( $lumps ), 			
			'gaji_baru'=>my_type_data_str( $gaji_baru ), 
		);
		my_insert_record('pr_kenaikan_gaji_karyawan' , $datas);	
		
		update_new_gapok($karyawan_id , $gaji_baru);
		log_kenaikan_gaji($karyawan_id ,$id , $gaji_lama, $gaji_baru);
		$datas = array(
			'proses'=>my_type_data_str('Y'),
		);
		my_update_record('pr_kenaikan_gaji','kenaikan_gaji_id',$id, $datas);		
	}
	if($pr_kenaikan_gajis['rapel']=="Y"){
		$rapel_id=get_rapel_id(1, $id) ;
		kalkulasi_rapel($rapel_id) ;
	}
	return true ;
}

function delete_exception($id , $karyawan_id){
	$query = "DELETE FROM pr_kenaikan_gaji_karyawan WHERE kenaikan_gaji_id = {$id} AND karyawan_id = {$karyawan_id}";
	return my_query($query);
}
function list_karyawan($id, $karyawan_id){
	$pr_kenaikan_gajis = my_get_data_by_id( 'pr_kenaikan_gaji' , 'kenaikan_gaji_id' , $id );
	$karyawans = loaddata_karyawan($karyawan_id);
	$jabatans = loaddata_jabatan($karyawans['jabatan_id']);
	$query = "SELECT * FROM pr_kenaikan_gaji_karyawan  
				WHERE kenaikan_gaji_id={$id}
				AND karyawan_id={$karyawan_id}";
			
	$result = my_query($query );
	$kenaikan_gajis=my_fetch_array($result ) ;	
	$grades=my_get_data_by_id('grade','grade_id',$kenaikan_gajis['grade_id']);
	
	$query = "
		SELECT struktur_gaji_id FROM pr_struktur_gaji_tahun 
		WHERE tanggal< '{$pr_kenaikan_gajis['tanggal']}'
		ORDER BY tanggal DESC LIMIT 1
	";
		
	$struktur_gajis=my_fetch_array(my_query($query));
	$struktur_gaji_id = isset( $struktur_gajis['struktur_gaji_id'] ) ? $struktur_gajis['struktur_gaji_id'] :  0;
	
	$query = "
		SELECT * FROM struktur_gaji_detail
		WHERE grade_id={$kenaikan_gajis['grade_id']}   
		AND struktur_gaji_id={$struktur_gaji_id} 
		LIMIT 1
	";
	$struktur_gaji_grades=my_fetch_array(my_query($query));
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'Uraian'=>array('style'=>'text-align:center;border-bottom:2px solid;width:45%'),   
		'Nilai'=>array('style'=>'border-bottom:2px solid;width:50%'),  
	);

	$row = array(); 
	$i = 1 ; 
	$row[] = array(
		'#'=>$i++,
		'ur'=>"NIK",
		'nilai'=>$karyawans['nik'], 
	);	
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Name",
		'nilai'=>$karyawans['nama_gelar'], 
	);	
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Hire",
		'nilai'=>date('d-m-Y',strtotime($karyawans['tmb'])), 
	);	
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Job Code",
		'nilai'=>$jabatans['nomor_posisi'], 
	);	
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Jabatan",
		'nilai'=>$jabatans['nama_jabatan'], 
	);	
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Golongan",
		'nilai'=>$karyawans['golongan_jabatan'], 
	);

	$row[] = array(
		'#'=>$i++,
		'ur'=>"Grade",
		'nilai'=>$grades['kode_grade'], 
	);
	$row[] = array(
		'#'=>$i++,
		'ur'=>"PA",
		'nilai'=>$kenaikan_gajis['pa'], 
	);
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Tabel Gaji - Min",
		'nilai'=>rp_format($struktur_gaji_grades['min']), 
	);
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Tabel Gaji - Mid",
		'nilai'=>rp_format($struktur_gaji_grades['mid']), 
	);
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Tabel Gaji - Max",
		'nilai'=>rp_format($struktur_gaji_grades['max']), 
	);
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Gaji Lama",
		'nilai'=>rp_format($kenaikan_gajis['gaji_lama']), 
	);
	if( (int) $kenaikan_gajis['gaji_lama'] == 0){
		$adj_tase=0;
	}else{	
		$adj_tase=((int) $kenaikan_gajis['adj'] - (int) $kenaikan_gajis['gaji_lama']) / (int) $kenaikan_gajis['gaji_lama'] ;
		$adj_tase=$adj_tase*100;
		$adj_tase=round($adj_tase,1);
	}	
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Adj/=",
		'nilai'=>rp_format($kenaikan_gajis['adj']).' ('.$adj_tase.' %)', 
	);
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Mark",
		'nilai'=>$kenaikan_gajis['mark2'], 
	);
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Lumpsum",
		'nilai'=>rp_format($kenaikan_gajis['lumps']), 
	);		
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Gaji kenaikan PA",
		'nilai'=>rp_format($kenaikan_gajis['gb_pa']), 
	);	
	$row[] = array(
		'#'=>$i++,
		'ur'=>"Cola",
		'nilai'=>rp_format($kenaikan_gajis['gaji_baru']), 
	);		
	$datas = table_rows($row); 
	$menuontop = array(  
		'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_kenaikan_gaji&task=list_detail&id='.$id.'\''),
	);
	$view  = form_header( "..." , "..."  );
	$view .=form_field_display( '<span class="label_form">'. $pr_kenaikan_gajis['sk_no'] .'</span>'   , "<b>Nomor SK</b>"    ) ;
	$view .=form_field_display( '<span class="label_form">'.  date( "m-Y" , strtotime($pr_kenaikan_gajis['tanggal'])) .'</span>'   , "<b>Periode</b>"    ) ;
	$view .=form_field_display( '<span class="label_form">'.  $pr_kenaikan_gajis['cola'].'%</span>'   , "<b>Cola</b>"    ) ;
	$view  .= form_footer(  );
	
	$printmenu = additional_menu_on_list( $menuontop , '');
	return $view.$printmenu. table_builder($header , $datas ,  7 , false  );	
}
	

function excel_report($id){
	my_component_load('xl_builder' , false);
	$pr_kenaikan_gajis = my_get_data_by_id( 'pr_kenaikan_gaji' , 'kenaikan_gaji_id' , $id );

	$query = "
		SELECT struktur_gaji_id FROM pr_struktur_gaji_tahun 
		WHERE tanggal< '{$pr_kenaikan_gajis['tanggal']}'
		ORDER BY tanggal DESC LIMIT 1
	";
	$struktur_gajis=my_fetch_array(my_query($query));
	$struktur_gaji_id = isset( $struktur_gajis['struktur_gaji_id'] ) ? $struktur_gajis['struktur_gaji_id'] :  0;
	
	$header = array(
		'#'=>array(), 
		'NIK'=>array(),   
		'Nama'=>array(),  
		'Hire'=>array(),  
		'Jobcode'=>array(),  
		'Jabatan'=>array(),  
		'Gol'=>array(),  
		'Grade'=>array(),  
		'PA'=>array(),  
		'Tabel Gaji - Min'=>array(),  
		'Tabel Gaji - Mid'=>array(),  
		'Tabel Gaji - Max'=>array(),  
		'Gaji Lama'=>array(),  
		'Adj/='=>array(),  
		'Adj-%'=>array(),  
		'Mark'=>array(),  
		'Lumpsum'=>array(),  
		'Gaji kenaikan PA'=>array(),  
		'Cola '.$pr_kenaikan_gajis[cola].'%'=>array(),  
	);
	if(isset($_GET['key']))
		$query = "SELECT * FROM pr_kenaikan_gaji_karyawan a 
			INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
			INNER JOIN biodata c ON c.biodata_id = b.biodata_id
			WHERE  a.kenaikan_gaji_id={$id} AND ( b.nik='{$_GET['key']}' OR c.nama LIKE '%{$_GET['key']}%'  )
			ORDER BY b.nik";
	else
		$query = "SELECT * FROM pr_kenaikan_gaji_karyawan a 
			INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
 			WHERE a.kenaikan_gaji_id={$id}
			ORDER BY b.nik";		

	$result = my_query($query );	
	$row = array(); 
	$i = 0; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawans = loaddata_karyawan($ey['karyawan_id']);
		$jabatans = loaddata_jabatan($karyawans['jabatan_id']);
		$query = "SELECT * FROM pr_kenaikan_gaji_karyawan  
					WHERE kenaikan_gaji_id={$id}
					AND karyawan_id={$ey['karyawan_id']}";				
		$res = my_query($query );
		$kenaikan_gajis=my_fetch_array($res ) ;	
		$grades=my_get_data_by_id('grade','grade_id',(int)$kenaikan_gajis['grade_id']);	

		$query = "
			SELECT * FROM struktur_gaji_detail
			WHERE grade_id={$kenaikan_gajis['grade_id']}   
			AND struktur_gaji_id={$struktur_gaji_id} 
			LIMIT 1
		";
		$struktur_gaji_grades=my_fetch_array(my_query($query));
		if( (int) $kenaikan_gajis['gaji_lama'] == 0){
			$adj_tase=0;
		}else{	
			$adj_tase=((int) $kenaikan_gajis['adj'] - (int) $kenaikan_gajis['gaji_lama']) / (int) $kenaikan_gajis['gaji_lama'] ;
			$adj_tase=$adj_tase*100;
			$adj_tase=round($adj_tase,1);			
		}	
		$row[] = array(
			'#'=> $i,  
			'nik'=> "'".sprintf('%07d',$ey['nik']),  
			'nama'=>$karyawans['nama_gelar'], 
			'hire'=>date('d-m-Y',strtotime($karyawans['tmb'])),
			'jobcode'=>$jabatans['nomor_posisi'], 
			'jabatan'=>$jabatans['nama_jabatan'], 
			'gol'=>$karyawans['golongan_jabatan'], 
			'grade'=>$grades['kode_grade'], 
			'pa'=>$kenaikan_gajis['pa'], 
			'min'=> $struktur_gaji_grades['min'] ,    
			'mid'=>  $struktur_gaji_grades['mid'] ,    
			'max'=> $struktur_gaji_grades['max'] ,     
			'gl'=>$kenaikan_gajis['gaji_lama'] ,   
			'adj'=> $kenaikan_gajis['adj']  ,    
			'adj_tase'=>  $adj_tase.' %',  
			'mark'=>$kenaikan_gajis['mark2'], 
			'lumpsum'=> $kenaikan_gajis['lumps'] ,   
			'gb_pa'=> $kenaikan_gajis['gb_pa'] ,   
			'cola'=> $kenaikan_gajis['gaji_baru'] 
		);
	}
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 	
}

function print_plain($id){	
	if(isset($_GET['key']))
		$query = "SELECT * FROM pr_kenaikan_gaji_karyawan a 
			INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
			INNER JOIN biodata c ON c.biodata_id = b.biodata_id
			WHERE ( b.nik='{$_GET['key']}' OR c.nama LIKE '%{$_GET['key']}%'  )
			ORDER BY b.nik";
	else
		$query = "SELECT * FROM pr_kenaikan_gaji_karyawan a 
			INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
			ORDER BY b.nik";		

	$result = my_query($query ); 
	$pr_kenaikan_gajis = my_get_data_by_id( 'pr_kenaikan_gaji' , 'kenaikan_gaji_id' , $id );

	$query = "
		SELECT struktur_gaji_id FROM pr_struktur_gaji_tahun 
		WHERE tanggal< '{$pr_kenaikan_gajis['tanggal']}'
		ORDER BY tanggal DESC LIMIT 1
	";
	$struktur_gajis=my_fetch_array(my_query($query));
	$struktur_gaji_id = isset( $struktur_gajis['struktur_gaji_id'] ) ? $struktur_gajis['struktur_gaji_id'] :  0;
	$datas = array();  
	$i=1;
	$datas[] = array(
		 'k1'=>array("No." ,3 ,STR_PAD_BOTH),
		 'k2'=>array("NIK" ,7,STR_PAD_RIGHT),
		 'k3'=>array("Nama" ,25,STR_PAD_RIGHT),
		 'k4'=>array("Grd." ,5,STR_PAD_RIGHT),  
		 'k5'=>array("Gaji Lama" ,10,STR_PAD_LEFT),  
		 'k6'=>array("Adj/=" ,10,STR_PAD_LEFT),  
		 'k7'=>array("Adj-%" ,5,STR_PAD_LEFT),  
		 'k8'=>array("PA" ,5,STR_PAD_RIGHT),  
		 'k9'=>array("Mark" ,5,STR_PAD_RIGHT),  
		 'k10'=>array("Lumpsum" ,10,STR_PAD_LEFT),  
		 'k11'=>array("Gaji PA" ,10,STR_PAD_LEFT),  
		 'k12'=>array('Cola '.$pr_kenaikan_gajis[cola].'%' ,10,STR_PAD_LEFT),  
	); 
	$datas[] = array(
		 'k1'=>array(" " ,100 ,STR_PAD_BOTH),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawans = loaddata_karyawan($ey['karyawan_id']);
		$jabatans = loaddata_jabatan($karyawans['jabatan_id']);
		$query = "SELECT * FROM pr_kenaikan_gaji_karyawan  
					WHERE kenaikan_gaji_id={$id}
					AND karyawan_id={$ey['karyawan_id']}";				
		$res = my_query($query );
		$kenaikan_gajis=my_fetch_array($res ) ;	
		$grades=my_get_data_by_id('grade','grade_id',$kenaikan_gajis['grade_id']);	

		$query = "
			SELECT * FROM struktur_gaji_detail
			WHERE grade_id={$kenaikan_gajis['grade_id']}   
			AND struktur_gaji_id={$struktur_gaji_id} 
			LIMIT 1
		";
		$struktur_gaji_grades=my_fetch_array(my_query($query));
		if( (int) $kenaikan_gajis['gaji_lama'] == 0){
			$adj_tase=0;
		}else{	
			$adj_tase=((int) $kenaikan_gajis['adj'] - (int) $kenaikan_gajis['gaji_lama']) / (int) $kenaikan_gajis['gaji_lama'] ;
			$adj_tase=$adj_tase*100;
			$adj_tase=round($adj_tase,1);
		}	
		$datas[] = array(
			 'k1'=>array($i ,3 ,STR_PAD_BOTH),
			 'k2'=>array($ey['nik'] ,7,STR_PAD_RIGHT),
			 'k3'=>array(substr($karyawans['nama_gelar'],0,25) ,25,STR_PAD_RIGHT),
			 'k4'=>array($grades['kode_grade'] ,5,STR_PAD_RIGHT),  
			 'k5'=>array(rp_format($kenaikan_gajis['gaji_lama']) ,10,STR_PAD_LEFT),  
			 'k6'=>array(rp_format($kenaikan_gajis['adj']) ,10,STR_PAD_LEFT),  
			 'k7'=>array($adj_tase.' %' ,5,STR_PAD_LEFT),  
			 'k8'=>array($kenaikan_gajis['pa'] ,5,STR_PAD_RIGHT),  
			 'k9'=>array($kenaikan_gajis['mark2'] ,5,STR_PAD_RIGHT),  
			 'k10'=>array(rp_format($kenaikan_gajis['lumps']) ,10,STR_PAD_LEFT),  
			 'k11'=>array(rp_format($kenaikan_gajis['gb_pa']) ,10,STR_PAD_LEFT),  
			 'k12'=>array(rp_format($kenaikan_gajis['gaji_baru']) ,10,STR_PAD_LEFT),  
		); 
	}
	$view = 	str_pad("CETAK LAPORAM KENAIKAN GAJI",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}


function form_pengecualian_kenaikan($id=0){ 
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');	
	  
	$view  = form_header( "detail pegawai" , "cp"  );

	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>(isset($_POST['nama_karyawan'])? $_POST['nama_karyawan'] : ( $fields['karyawan_id'] > 0 ? $karyawan_data['nik'].'/'.$karyawan_data['nama'] :'') ),
		'id'=>'nama_karyawan',
		'type'=>'textfield',
		'size'=>'45'
	);
	$form_karyawan = form_dynamic($karyawan);
	$view .= form_field_display( $form_karyawan  , "<b>NIK/Nama Kary.</b>"  );
	

	$adj = array(
		'name'=>'adj',
		'value'=>(isset($_POST['adj'])? $_POST['adj'] :   '' ),
		'id'=>'adj',
		'type'=>'textfield',
		'size'=>'45'
	);
	$form_adj = form_dynamic($adj);
	$view .= form_field_display( $form_adj  , "<b>Adj.</b>"  );	
	
	$gaji_baru = array(
		'name'=>'gaji_baru',
		'value'=>(isset($_POST['gaji_baru'])? $_POST['gaji_baru'] :   '' ),
		'id'=>'gaji_baru',
		'type'=>'textfield',
		'size'=>'45'
	);
	$form_gaji_baru = form_dynamic($gaji_baru);
	$view .= form_field_display( $form_gaji_baru  , "<b>Gaji baru.</b>"  );	
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	
	return $view;
}

function submit_pengecualian($id){
	$karyawan_id =  check_karyawan_by_nik_nama($_POST['nama_karyawan']); 
	$query = "DELETE FROM pr_kenaikan_gaji_karyawan WHERE karyawan_id = {$karyawan_id} AND kenaikan_gaji_id={$id}  ";
	my_query($query);
	$kekaryawanan = my_get_data_by_id('kekaryawanan','karyawan_id',$karyawan_id);
	$datas = array(
			'kenaikan_gaji_id'=>my_type_data_int($id),
			'karyawan_id'=>my_type_data_int($karyawan_id),
			'grade_id'=>my_type_data_int($kekaryawanan['grade_id']),
			'gaji_lama'=>my_type_data_str( $kekaryawanan['gapok_km'] ), 			
			'adj'=>my_type_data_str( $_POST['adj'] ), 			
			'pa'=>my_type_data_str( '' ), 			
			'mark2'=>my_type_data_str( 'TP' ), 			
			'gb_pa'=>my_type_data_str('' ), 			
			'lumps'=>my_type_data_str( 0 ), 			
			'gaji_baru'=>my_type_data_str( $_POST['gaji_baru'] ), 
		);
		my_insert_record('pr_kenaikan_gaji_karyawan' , $datas);	
		log_kenaikan_gaji($karyawan_id ,get_periode_gaji(), $kekaryawanan['gapok_km'],  $_POST['gaji_baru']);
		$datas2 = array(
			'gapok_km'	=> my_type_data_str($gapok_br)
		);
		
		return my_update_record(	'kekaryawanan'  ,	'karyawan_id'	,	$karyawan_id	,	$datas2 ); 
}

function exception_grid_data($id){
	$header = array( 
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),   
		'Nama'=>array('style'=>'text-align:center;border-bottom:2px solid;width:35%'), 	    
		'Grade'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),     
		'Gaji Lama'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),      
		'Cola '=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),  
		'  '=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
	);

	$query = "SELECT * FROM pr_kenaikan_gaji_karyawan a 
			INNER JOIN karyawan b ON b.karyawan_id=a.karyawan_id 
			WHERE mark2 ='TP'
			ORDER BY b.nik";		

	$result = my_query($query );	
	$row = array(); 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$grades=my_get_data_by_id('grade','grade_id',$ey['grade_id']);	
		$detailproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=exception_page_delete&id='.$id.'&karyawan_id='.$ey['karyawan_id'],
				'title'=>'Delete exception'
		);
		$detail_button = button_icon( 'b_drop.png' , $detailproperty  );

		$row[] = array( 
			'nik'=>position_text_align( $ey['nik'], 'center'),
			'nama'=>$karyawan['nama_gelar'],  
			'grade'=>position_text_align($ey['grade_id'], 'right'),    
			'gaji_lama'=>position_text_align( rp_format($ey['gaji_lama']) , 'right'),   
			'gaji_baru'=>position_text_align( rp_format($ey['gaji_baru']) , 'right'),   
			' v'=>position_text_align($detail_button,'right'),
			);
	}
	$datas = table_rows($row); 
	$menuontop = array(  
		'Add Exception' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=exception_page_form&id='.$id.'\''),
	); 
	$search ='';
	$printmenu = additional_menu_on_list( $menuontop , $search);
	return $printmenu . table_builder($header , $datas , 10 ,false );
}

function revert($kenaikan_gaji_id){
	$query = "SELECT karyawan_id , gapok_lama ,gapok_baru FROM pr_kenaikan_gaji_log WHERE kenaikan_gaji_id = {$kenaikan_gaji_id}
GROUP BY karyawan_id  ORDER BY `pr_kenaikan_gaji_log`.`karyawan_id` ASC";
	$result = my_query($query);
	while($row = my_fetch_array($result)){
		$query_update = "UPDATE kekaryawanan SET gapok_km ='{$row['gapok_lama']}' WHERE karyawan_id = {$row['karyawan_id']}";
		 
		my_query($query_update); 
	}
}
  